package controllers.api.v1.base;

import controllers.api.v1.auth.ApiAuth;
import controllers.license.LicenseChecker;
import facade.AppContext;
import facade.open.BaseJsonAPI;
import facade.open.request.base.AdminMenuRequest;
import facade.open.response.base.AdminMenuResponse;
import facade.vo.base.AdminParentMenuVO;
import models.base.AdminMenu;
import models.base.AdminUser;
import models.constants.DeletedStatus;
import play.Logger;
import play.data.validation.Required;
import play.i18n.Messages;
import play.mvc.With;

import javax.validation.Valid;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 菜单功能
 */
@With({ApiAuth.class, LicenseChecker.class})
public class AdminMenuAPI extends BaseJsonAPI {


    /**
     * 查询所有菜单列表
     */
    public static void allMenus(String businessId) {

        List<AdminMenu> menuList = AdminMenu.findAllMenu(businessId);
        Map<String, Object> dataMap = new HashMap<>();
        if(menuList.size() == 0 ){
            dataMap.put("rows",menuList);
            dataMap.put("flag",false);
            dataMap.put("total",0);
        }else{
            dataMap.put("rows",menuList);
            dataMap.put("flag",true);
            dataMap.put("total",menuList.size());
        }
        renderJSON(dataMap);
    }

    //新建
    public static void create() {
        Logger.info("LOGGER AdminMenuAPI 0225164700002 create 创建菜单-----");
        //权限验证
//        if (!checkAuth("AdminBusinessAPI", "create"))
//            renderWarnMessage(407, Messages.get("flash.warn.no-permission"));

        AdminMenuRequest request = validateRequestBody("menu", AdminMenuRequest.class);

        Boolean exist = AdminMenu.existByTitle(request.title);
        if(exist)
            renderWarnMessage(401, "标题为["+request.title+"]的菜单已添加，勿重复添加！");

        AdminMenu menu = AdminMenu.create(request);
        flash.put("notice", Messages.get("flash.notice.save-success"));
        AdminMenuResponse response = AdminMenuResponse.from(menu);
        renderData(response);

    }

    /**
     * 显示详情.
     */
    public static void show(@Valid @Required String id) {
//        Corp corp = AppContext.currentCorp();
        AdminMenu menu = AdminMenu.findOne(id);
        if (menu == null)
            renderWarnMessage(404, "菜单不存在，可能已经被删除！");
        AdminMenuResponse response = AdminMenuResponse.from(menu);
        renderData(response);
    }

    /**
     * 查询父节点菜单信息.
     */
    public static void parentMenu(@Valid @Required String parentId) {
//        Corp corp = AppContext.currentCorp();
        AdminMenu menu = AdminMenu.findOne(parentId);
        if (menu == null)
            renderWarnMessage(404, "菜单不存在，可能已经被删除！");
        AdminMenuResponse response = AdminMenuResponse.from(menu);

        AdminParentMenuVO parentVO = new AdminParentMenuVO();
        parentVO.parentId = menu.id;
        parentVO.parentTitle = menu.title;
        renderData(parentVO);

    }


    //修改
    public static void update(@Valid @Required String id) {
        Logger.info("LOGGER AdminMenuAPI 0225164700003 modify 修改菜单-----");

//        if (!checkAuth("AdminBusinessAPI", "modfiy"))
//            renderWarnMessage(407, Messages.get("flash.warn.no-permission"));

        AdminMenuRequest request = validateRequestBody("business", AdminMenuRequest.class);

        if (request == null) {
            renderWarnMessage(404, Messages.get("warn.no-exists","菜单"));
        }
        AdminMenu menu = AdminMenu.findOne(id);

        if (menu == null) {
            renderWarnMessage(404, Messages.get("warn.no-exists","菜单"));
        }
        notFoundIfNull(menu);

        AdminMenu.update(menu, request);
        AdminMenuResponse response = AdminMenuResponse.from(menu);
        flash.put("notice", Messages.get("flash.notice.update-success"));


        renderData(response);

    }

    /**
     * 删除
     */
    public static void delete(@Valid @Required String id) {
        Logger.info("LOGGER AdminMenuAPI 0225164700003 delete 删除菜单-----");
        Logger.info("--------------- 删除的菜单id :%s", id);

//        if (!checkAuth("AdminBusinessAPI", "delete"))
//            renderWarnMessage(407, Messages.get("flash.warn.no-permission"));

        AdminUser loginUser = AppContext.currentUser();
        Logger.info("LOGGER AdminMenuAPI   delete   0223163700001  删除菜单功能 ，操作人:%s , 操作人ID： %s", loginUser != null?loginUser.name: "" , loginUser);

        AdminMenu menu = AdminMenu.findOne(id);
        if (menu != null) {
            menu.deleted = DeletedStatus.DELETED;
            menu.modifyAt = new Date();
            menu.modifyUser = loginUser;
            menu.save();

        }
        flash.put("notice", Messages.get("flash.notice.delete-success", "菜单功能"));

        renderSuccMessage("删除成功！");

    }


}
